/*
 * Copyright 2008 Carnegie Mellon University
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, 
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package edu.cmu.lti.sepia.api.html;

import java.util.List;

import edu.cmu.lti.sepia.data.db.IRJudgmentResultDAO;
import edu.cmu.lti.sepia.data.db.PoolDAO;
import edu.cmu.lti.sepia.data.db.QAJudgmentResultDAO;
import edu.cmu.lti.sepia.data.db.SQL;
import edu.cmu.lti.sepia.data.db.TopicDAO;
import edu.cmu.lti.sepia.infra.eval.model.IRJudgmentResult;
import edu.cmu.lti.sepia.infra.eval.model.Pool;
import edu.cmu.lti.sepia.infra.eval.model.QAJudgmentResult;
import edu.cmu.lti.sepia.infra.topic.model.Task;
import edu.cmu.lti.sepia.infra.topic.model.Topic;
import edu.cmu.lti.sepia.util.ExceptionUtil;

public class JudgmentProgressGenerator {
	
	public static String printTable( SQL sql, Task task, boolean irResult ) {
	  try {
  	  List<Topic> topics = TopicDAO.getGoldStandard( sql, task.getTaskNameShort(), -1, null );
      TopicDAO.removeSimilarNuggets(topics);
      TopicDAO.generateExternalIds(topics,task.getTrgLang(),4);
      TopicDAO.removeUnselected(topics);
      
      List<Pool> pools = PoolDAO.getPools(sql, task.getTaskNameShort(), irResult);
      
      StringBuilder sb = new StringBuilder();
      
      sb.append( "<h3>"+task.getFullName()+"</h3><br>" );
      if (topics==null || topics.size()==0 || pools==null || pools.size()==0 ) {
        sb.append("N/A\n<br><br>");
        return sb.toString();
      }
      
      sb.append( "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" class=\"datatable\">" );
      
      //header
      sb.append( "<thead><tr><th>Topics</th>" );
      for (Pool pool : pools) {
        sb.append( "<th>"+pool.getPoolName()+"</th>" );
      }
      sb.append( "</tr></thead>\n" );
      
      sb.append( "<tbody>\n" );
      for (int i=0;i<topics.size(); i++) {
        Topic topic = topics.get(i); 
        sb.append("<tr class=\""+(i%2==1?"odd":"even")+"\"><td>"+topic.getExternalId()+"</td>");
        for (Pool pool : pools) {
          if (irResult) {
            List<IRJudgmentResult> results = IRJudgmentResultDAO.findResultsByPoolAndTopic(sql, task.getTaskNameShort(), pool.getPoolId(), topic.getExternalId(), -1);
            int denom = results.size();
            int num = 0;
            for (IRJudgmentResult r : results) {
              if ( r.isEvaluatedByOthers() ) num++;
            }
            sb.append("<td align=\"right\">"+num +" / "+denom+"</td>");
          } else {
            List<QAJudgmentResult> results = QAJudgmentResultDAO.findResultsByTopicAndPool(sql, task.getTaskNameShort(), topic.getExternalId(), pool.getPoolId());
            int denom = results.size();
            int num = 0;
            for (QAJudgmentResult r : results) {
              if ( r.getUserId()!=-1 ) num++;
            }
            sb.append("<td align=\"right\">"+num +" / "+denom+"</td>");
          }
        }
        sb.append("</tr>\n");
      }
      sb.append( "</tbody>" );
      sb.append( "</table>\n" );
      sb.append( "<br>\n" );
  		return sb.toString();
	  } catch (Exception e) { 
	    return ExceptionUtil.exceptionToString(e);
	  }
	}
	}
